"""
Case Type   : 功能测试
Case Name   : 恢复数据库
Create At   : 2024.04.29
Owner       : @wan005
Description :
    1、查询当前数据库
    2、删除指定编码数据库并创建新的默认库
Expect      :
    1、成功
    2、删除指定编码的数据库、创建新的默认库成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class CompatibilityTest002(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '----step1:查询当前数据库; expect:成功----'
        self.log.info(text)
        sql_cmd = "select current_database();"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.user_node.db_name, sql_res, "执行失败" + text)

    def tearDown(self):
        text = '----step2:删除指定编码数据库并创建新的默认库;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.user_node.db_name};" \
                  f"create database {self.user_node.db_name};"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(
               sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
